﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;
using NBlock.Core;
using System.Linq.Expressions;

namespace Cost.Controllers
{
    public class MaterialDeliveryPlanController : Controller
    {
        #region 构造函数
        private readonly ICostRepository<V_MaterialDeliveryPlan> materialdeliveryplanRepository;

        public MaterialDeliveryPlanController(ICostRepository<V_MaterialDeliveryPlan> materialdeliveryplanRepository)
        {
            this.materialdeliveryplanRepository = materialdeliveryplanRepository;
        }
        #endregion

        public ActionResult Index()
        {
            ViewBag.Date = DateTime.Now.ToString("yyyy-MM-dd");
            return View();
        }

        [HttpPost]
        public JsonResult List(DateTime? date, nbPageInfo pager, nbSearchCondition where)
        {
            Expression<Func<V_MaterialDeliveryPlan, bool>> filter = null;
            if (date.HasValue)
                filter = m => m.Date == date;

            var pageData = materialdeliveryplanRepository.GetPageable(pager, filter, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult SetectType()
        {
            var types = (from plan in materialdeliveryplanRepository.FindAll()
                         select new DeliveryTypeViewModel
                         {
                             SiteId = plan.SiteId,
                             Date = plan.Date,
                         }).Distinct().ToList();

            foreach (var type in types)
            {
                type.SiteName = DependencyResolver.Current.GetService<IEnumService>().GetEnumText("ProjectSite", type.SiteId);
            }

            return View(types);
        }
    }

    public class DeliveryTypeViewModel
    {
        public string SiteId { get; set; }
        public string SiteName { get; set; }
        public DateTime Date { get; set; }
        public string DateString { get { return Date.ToString("yyyy-MM-dd"); } }
    }
}
